package com.share.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.share.config.GiteeImgBedConfig;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ImageUtils
 * @Description: 图片上传工具类
 * @author: hxt
 * @date 2020/9/14 10:19
 * @Version: 1.0
 **/
public class ImageUtils {
    
    /**
     * 上传图片到Gitee，根据Flag类型的不同，上传到码云的文件夹
     *
     *  返回值：返回图片的网络地址
     */
    public static String upload(MultipartFile file, Integer userId)throws IOException {
        String trueFileName = file.getOriginalFilename();
        System.out.println("trueFileName = " + trueFileName);
        assert trueFileName != null;
        String suffix = trueFileName.substring(trueFileName.lastIndexOf("."));

        String paramImgFile = Base64.encode(file.getBytes());
        //转存到gitee
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", GiteeImgBedConfig.ACCESS_TOKEN);
        paramMap.put("message", GiteeImgBedConfig.CREATE_REPOS_MESSAGE);
        paramMap.put("content", paramImgFile);

        String targetDir = "";

        targetDir = userId + "/" + IdUtil.simpleUUID() + suffix;

        String requestUrl = String.format(GiteeImgBedConfig.CREATE_REPOS_URL, GiteeImgBedConfig.OWNER,
                GiteeImgBedConfig.REPO_NAME, targetDir);

        String resultJson = HttpUtil.post(requestUrl, paramMap);

        if (JSONUtil.parseObj(resultJson).getObj("commit") != null) {
            return GiteeImgBedConfig.GITPAGE_REQUEST_URL + targetDir;
        }
        return null;
    }

    /**
     * 上传图片到码云
     */
    public static String upload(File file, Integer userId)throws IOException {
        String trueFileName = file.getName();
        System.out.println("trueFileName = " + trueFileName);
        assert trueFileName != null;
        String suffix = trueFileName.substring(trueFileName.lastIndexOf("."));

        String paramImgFile = Base64.encode(file);
        //转存到gitee
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", GiteeImgBedConfig.ACCESS_TOKEN);
        paramMap.put("message", GiteeImgBedConfig.CREATE_REPOS_MESSAGE);
        paramMap.put("content", paramImgFile);

        String targetDir = "";

        targetDir = userId + "/" + IdUtil.simpleUUID() + suffix;

        String requestUrl = String.format(GiteeImgBedConfig.CREATE_REPOS_URL, GiteeImgBedConfig.OWNER,
                GiteeImgBedConfig.REPO_NAME, targetDir);

        String resultJson = HttpUtil.post(requestUrl, paramMap);

        if (JSONUtil.parseObj(resultJson).getObj("commit") != null) {
            return GiteeImgBedConfig.GITPAGE_REQUEST_URL + targetDir;
        }
        return null;
    }

    /**
     * 获得随机的头像
     */
    public static String getRandomImg(){
        String[] imgs = {
                "https://gitee.com/huang-xiangting/hxt-img/raw/master/other/1.jpg",
                "https://gitee.com/huang-xiangting/hxt-img/raw/master/other/2.jpg",
                "https://gitee.com/huang-xiangting/hxt-img/raw/master/other/3.jpg",
                "https://gitee.com/huang-xiangting/hxt-img/raw/master/other/4.jpg",
                "https://gitee.com/huang-xiangting/hxt-img/raw/master/other/5.jpg",
                };
        return imgs[(int) (Math.random() * 5)];
    }




    public static boolean checkFileSize(Long len, int size, String unit) {
        double fileSize = 0;
        if ("B".equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if ("K".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if ("M".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if ("G".equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }
}
